import { Inject, Provide } from "@midwayjs/decorator";
import { RedisService } from "@midwayjs/redis";
import { Tools } from "../common/Tools";

import Dysmsapi20170525, * as $Dysmsapi20170525 from "@alicloud/dysmsapi20170525";
import * as $OpenApi from "@alicloud/openapi-client";
import { IBatchSMSData, ISMSData } from "../interfaces/ISMS.interface";
import { ErrCode } from "../common/ErrCode";
import { SystemConfigService } from "./system.service";
import { IAliSMSTemplate, SystemConfigType } from "../interfaces/ISystem.interface";

@Provide()
export class SMSServices{
    @Inject()
    redisService:RedisService;

    @Inject()
    systemService:SystemConfigService;

    /**初始化sdk */
    private async init(appid:string=null):Promise<Dysmsapi20170525>{
            let config = new $OpenApi.Config({
                 // 您的AccessKey ID
                accessKeyId: 'LTAI5t861yCJS16azetBb7zK',
                // 您的AccessKey Secret
                accessKeySecret: 'LX2lo47mBEYK4YUXE86aVXiTnI8fAD',
                // 访问的区域
                // regionId: 'RegionId'
            });
            config.endpoint = 'dysmsapi.aliyuncs.com';
            return (new Dysmsapi20170525(config));
    }

    public async SendSms(data:ISMSData,appid:string=null):Promise<any>{
        let client:Dysmsapi20170525 =  await this.init(appid);
        let senddata =  new $Dysmsapi20170525.SendSmsRequest(data); 
        let result = await client.sendSms(senddata);
        return result.body.code == 'OK'?ErrCode.SYSTEM_SUCCESS:ErrCode.SYSTEM_FAIL;
    }

    public async SendBatchSms(data:IBatchSMSData,appid:string = null):Promise<any>{
        let client:Dysmsapi20170525 = await this.init(appid);
        let senddata = new $Dysmsapi20170525.SendBatchSmsRequest(data);
        let result = await client.sendBatchSms(senddata);
        return result.body.code == 'OK'?ErrCode.SYSTEM_SUCCESS:ErrCode.SYSTEM_FAIL;
    }

    private makeRedisKey(value){
        return "phone_code_"+value;
    }

    private async makeCode(phone:string):Promise<string>{
        let code:string = Tools.makeRandomNum(6);
        this.redisService.set(this.makeRedisKey(phone),code);
        return code;
    }

    /**获取手机验证码 */
    public async getPhoneCode(phone:string):Promise<ErrCode>{
        let code:string = await this.makeCode(phone);
        let templateInfo:IAliSMSTemplate = await this.systemService.getSystemConfig(SystemConfigType.SMS_ALI);
        if(templateInfo == null){//默认 设置
            templateInfo = {
                idCode:{
                    signName:"乐家中国",
                    templateCode:"SMS_93770021"
                }
            }
        }
        let reqdata:ISMSData = {
            phoneNumbers:phone,
            signName:templateInfo.idCode.signName,//"乐家中国",
            templateCode:templateInfo.idCode.templateCode,//"SMS_93770021",
            templateParam:JSON.stringify({code:code})
        }
        return await this.SendSms(reqdata);
    }
    /**验证手机验证码 */
    public async checkPhoneCode(phone:string,code:string):Promise<ErrCode>{
        let rkey:string = this.makeRedisKey(phone);
        let vcode:string = await this.redisService.get(rkey);
        if(vcode != null){
            if(vcode == code){
                return ErrCode.SYSTEM_SUCCESS;
            }else{
                return ErrCode.USER_BIND_CODE_ERR;
            }
        }else{
            return ErrCode.USER_BIND_CODE_NULL;
        }
    }
}